package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

const (
	DSN = "root:123456@(127.0.0.1:3306)/gin_class?charset=utf8mb4&parseTime=True&loc=Location"
)

func main() {
	initTable()
}

func initTable() {
	db := initDB()

	db.AutoMigrate(&User{}, &CreditCard{})
}

//初始化数据源
func initDB() *gorm.DB {
	db, err := gorm.Open(mysql.Open(DSN), &gorm.Config{})
	if err != nil {
		fmt.Println(err)
		recover()
	}
	return db
}

type User struct {
	gorm.Model
	Name string `gorm: "index"`
	//修改外键的关联字段
	CreditCard CreditCard `gorm: "foreignKey:UserName; references:name"`
}

func (User) TableName() string {
	return "t_has_one_user"
}

type CreditCard struct {
	gorm.Model
	Number   string
	UserName string
}

func (CreditCard) TableName() string {
	return "t_has_one_credit_card"
}
